#!/bin/bash

# Author owner: chenqiangzhishen@163.com

# TODO: please use an IP that never use in your env
export VIP='7.0.144.186'
# TODO: maybe you should use eth0|1 interface in your env
export INTERFACE=ens160
export MYSQL_IMAGE=mysql:5.7
export KEEPALIVED_IMAGE=solnetcloud/keepalived:1.2.7
export MYSQL_NAME=mysql_master
export KEEPALIVED_NAME=keepalived
export SERVER_ID=161733388

function fail() {
    echo "$@"
    exit 1
}

echo "start to install mysql keepalived..."
sudo service docker restart
cd /data/install
sudo docker load -i dockercontainer-mysql-5.7.tar
sudo docker load -i dockercontainer-solnetcloud-keepalived.tar
echo "start to deploy master...."
if [ ! -d /etc/mysql-custom ]; then
    sudo mkdir -p /etc/mysql-custom;
fi
if [ -f mysql13388.cnf ]; then
    sudo cp -f mysql13388.cnf /etc/mysql-custom/;
else
    fail "[FAIL]: please put mysql13388.cnf in the current dir";
fi

# rm mysql data file
sudo rm -rf        /data/mysql/*
sudo mkdir -p      /data/mysql
sudo mkdir -p      /data/log/mysql
sudo chmod -R 777  /data/mysql
sudo chmod -R 777  /data/log
echo "运行mysql docker container..."
sudo docker run --name $MYSQL_NAME -v /etc/localtime:/etc/localtime:ro -v /etc/mysql-custom:/etc/mysql/conf.d -v /data/mysql:/var/lib/mysql -v /data/log/mysql:/var/log -e MYSQL_ROOT_PASSWORD=hadoop -p 3306:3306 -p 13388:13388 -d mysql:5.7 mysqld --server-id=$SERVER_ID > /dev/null 2>&1
[[ $? == 0 ]] && echo "[OK]: docker run mysql_master" || fail "[FAIL]: docker run mysql_master"
# waiting for mysql master create finished
sleep 20
#wzt
echo "root 用户连接授权"
sudo docker exec $MYSQL_NAME /usr/bin/mysql -uroot -phadoop -AN -e 'GRANT ALL PRIVILEGES ON *.* TO "root"@"%" IDENTIFIED BY "hadoop" WITH GRANT OPTION;' > /dev/null 2>&1 
[[ $? == 0 ]] && echo "[OK]: mysql grant all to root" || fail "[FAIL]: mysql grant all to root"
sudo docker exec $MYSQL_NAME /usr/bin/mysql -uroot -phadoop -AN -e 'flush privileges;' > /dev/null 2>&1
[[ $? == 0 ]] && echo "[OK]: mysql flush privileges" || fail "[FAIL]: mysql flush privileges"
echo "复制用户数据备份授权"
sudo docker exec $MYSQL_NAME /usr/bin/mysql -uroot -phadoop -AN -e 'GRANT REPLICATION SLAVE ON *.* TO "replica"@"%" IDENTIFIED BY "repl_slave";' > /dev/null 2>&1
[[ $? == 0 ]] && echo "[OK]: mysql grant replication slave" || fail "[FAIL]: mysql grant replication slave"
sudo docker exec $MYSQL_NAME /usr/bin/mysql -uroot -phadoop -AN -e 'flush privileges;' > /dev/null 2>&1
[[ $? == 0 ]] && echo "[OK]: mysql flush privileges" || fail "[FAIL]: mysql flush privileges"
#delete vip dev to avoid old vip existed
sudo ip a | grep $VIP > /dev/null 2>&1
[[ $? == 0 ]] && sudo ip a del $VIP dev $INTERFACE > /dev/null 2>&1
echo "运行 keepalived..."
if [ ! -d /data/keepalived ]; then
    sudo mkdir -p /data/keepalived;
    sudo cp check-mysql-status.sh /data/keepalived;
    sudo chmod -R 777 /data/keepalived;
fi
#vrid 0-255,从52开始，51是默认值
#下面的启动 keeplive的命令在UAT上面测试通过
#master
#sudo docker run --name=keep04 --net=host --privileged=true --volume=$PWD/:/ka-data/scripts/ -d solnetcloud/keepalived:1.2.7 \
#--master --override-check check-mysql-status.sh --enable-check --auth-pass pass --vrid 52 ens160 102 $VIP/24/ens160
#slave
#sudo docker run --name=keep04 --net=host --privileged=true --volume=$PWD/:/ka-data/scripts/ -d solnetcloud/keepalived:1.2.7 \
#--override-check check-mysql-status.sh --enable-check --auth-pass pass --vrid 52 ens160 99 $VIP/24/ens160

sudo docker run --name=$KEEPALIVED_NAME --net=host --privileged=true --volume=/data/keepalived:/ka-data/scripts -d $KEEPALIVED_IMAGE --master --override-check check-mysql-status.sh --enable-check --auth-pass pass --vrid 52 $INTERFACE 101 $VIP/24/$INTERFACE > /dev/null 2>&1
[[ $? == 0 ]] && echo "[OK]: docker run keepalived" || fail "[FAIL]: docker run keepalived"
echo "================================="
echo "[OK]: master deploy finished"
